package com.nobug.run;

import com.nobug.CaptureScreen;
import com.nobug.GlobalConfig;
import com.nobug.MyRobot;
import com.nobug.shudu.Matrix;
import com.nobug.shudu.ShuduUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


/**
 * @author 389561407@qq.com
 * @version 1.0
 * @since 2022-12-27
 */
public class MainRunSimple {

    //用于第二次 鼠标位置 记录休闲游戏坐标
    static int x = 0;
    static int y = 0;


    public static void main(String[] args)  {


        //鼠标移动到数独第一个格子正中间，不要动运行此方法
//        mouseRes();

        while (true){
            if(x == 0 && y == 0){
                int[] mouseXY = CaptureScreen.getMouseXY(3);
                x = mouseXY[0];
                y = mouseXY[1];
            }
            MyRobot.move(x,y);
            //输入样本图片，找到显示器上的位置进行左键点击操作
            int[] imageLButton = findImageLButton("simple.png");
            MyRobot.move(imageLButton[0]+387,imageLButton[1]+39);
            MyRobot.lButton();
            MyRobot.lButton();


            MyRobot.move(imageLButton[0]+387-115,imageLButton[1]+39-123);
            MyRobot.lButton();

            //简单级别启动
            System.out.println("简单级别启动");
            int i = mouseResFor();
            if(i != 0){
                System.out.println("移动到X上点击关闭");
                MyRobot.move(imageLButton[0]+387-115+500,imageLButton[1]+39-123-66);
                MyRobot.lButton();
                MyRobot.move(imageLButton[0]+387-115+500-340,imageLButton[1]+39-123-66+250);
                findImageLButton("qd.png");
                continue;
            }
            //简单级别结束

            //点击继续
            MyRobot.move(imageLButton[0]+387-115+158,imageLButton[1]+39-123+181);
            MyRobot.lButton();

            //定位到头部开始
            MyRobot.move(imageLButton[0]+387-115,imageLButton[1]+39-123);
            MyRobot.lButton();
            MyRobot.sleep(1000);
            //第二轮启动识别
            System.out.println("第二轮启动识别");
            int ii = mouseResFor();
            if(ii != 0){
                System.out.println("移动到X上点击关闭");
                MyRobot.move(imageLButton[0]+387-115+500,imageLButton[1]+39-123-66);
                MyRobot.lButton();
                MyRobot.move(imageLButton[0]+387-115+500-340,imageLButton[1]+39-123-66+250);
                findImageLButton("qd.png");
                continue;
            }


            //点击继续
            MyRobot.move(imageLButton[0]+387-115+158,imageLButton[1]+39-123+181);
            MyRobot.lButton();

            //定位到头部开始
            MyRobot.move(imageLButton[0]+387-115,imageLButton[1]+39-123);
            MyRobot.lButton();
            MyRobot.sleep(1000);
            //第三轮
            System.out.println("第三轮启动识别");
            int iii = mouseResFor();
            if(iii != 0){
                System.out.println("移动到X上点击关闭");
                MyRobot.move(imageLButton[0]+387-115+500,imageLButton[1]+39-123-66);
                MyRobot.lButton();
                MyRobot.move(imageLButton[0]+387-115+500-340,imageLButton[1]+39-123-66+250);
                findImageLButton("qd.png");
                continue;
            }

            //点击确定 结束
            MyRobot.move(imageLButton[0]+387-115+243,imageLButton[1]+39-123+184);
            MyRobot.sleep(1000);
            MyRobot.lButton();
            MyRobot.lButton();
        }

    }

    private static int mouseResFor() {
        return mouseRes();
    }


    public static int[] findImage(String fileName) {

        File file = new File(GlobalConfig.IMAGE_FIND, fileName);

        BufferedImage read = null;
        try {
            read = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //如果要找的图片是
        int height = read.getHeight();
        int width = read.getWidth();
        int max = Math.max(height, width);

        int[] startXY = CaptureScreen.getImageXYWH(max << 1);

        //根据样本在显示器上找到一个坐标
        int[] mouseXYByImage = CaptureScreen.getMouseXYByImage(read, startXY);
        return mouseXYByImage;
    }

    public static int[] findImageLButton(String fileName) {

        File file = new File(GlobalConfig.IMAGE_FIND, fileName);

        BufferedImage read = null;
        try {
            read = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //如果要找的图片是
        int height = read.getHeight();
        int width = read.getWidth();
        int max = Math.max(height, width);

        int[] startXY = CaptureScreen.getImageXYWH(max << 1);

        //根据样本在显示器上找到一个坐标
        int[] mouseXYByImage = CaptureScreen.getMouseXYByImage(read, startXY);
        if(mouseXYByImage[0] == 0&&  mouseXYByImage[1]==0){
            return mouseXYByImage;
        }

        MyRobot.move(mouseXYByImage[0]+(width>>1),mouseXYByImage[1]+(height>>1));
        MyRobot.lButton();
//        mouseXYByImage[0] = mouseXYByImage[0]+(width>>1);
//        mouseXYByImage[1] = mouseXYByImage[1]+(height>>1);
        return mouseXYByImage;
    }

    static int times = 0;
    static int[] xIndex = {0,0,1,-2,0,0,2,0};
    static int[] yIndex = {1,-2,1,0,-1,2,0,-2};

    private static int mouseRes() {
        int[] startXY = CaptureScreen.getImageXYWH(26);

        Matrix matrix = ShuduUtil.getMatrix();
        try {
            CaptureScreen.createScreen(startXY,matrix);
        } catch (AWTException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("数据读取完毕：");
        matrix.print();
        Matrix res = ShuduUtil.getRes(matrix);
        if(res == matrix && times<8){
            //偏移初始位置再做尝试
            System.out.println("偏移初始位置再做尝试");
            Point location = MouseInfo.getPointerInfo().getLocation();
            MyRobot.move(location.x+xIndex[times],location.y+yIndex[times]);
            times++;
            System.out.println("重试次数："+times);
            mouseRes();
        }
        if(times >=8){
            //关闭重新来
            times = 0;
            System.out.println("关闭重新来");
            return -1;
        }
        times = 0;
        System.out.println("结果计算完毕：");
        res.print();
        //开始调用电脑键盘进行输入
        int[][] ints = res.toArray();

        for (int i = 0; i < ints.length; i++) {
            for (int j = 0; j < ints[0].length; j++) {
                if(ints[i][j] == 0){
                    return -2;
                }
            }
        }
        System.out.println("开始操作请勿移动鼠标：");
        int[][] ints1 = matrix.toArray();
        MyRobot.print(ints,ints1);
        return 0;
    }

    public static void runMain() {

        System.out.println("请把鼠标放置在休闲游戏上不要动！");
        System.out.println("请把鼠标放置在休闲游戏上不要动！");
        System.out.println("请把鼠标放置在休闲游戏上不要动！");

        while (true){
            if(x == 0 && y == 0){
                int[] mouseXY = CaptureScreen.getMouseXY(3);
                x = mouseXY[0];
                y = mouseXY[1];
            }
            MyRobot.move(x,y);
            //输入样本图片，找到显示器上的位置进行左键点击操作
            int[] imageLButton = findImageLButton("simple.png");
            MyRobot.move(imageLButton[0]+387,imageLButton[1]+39);
            MyRobot.lButton();
            MyRobot.lButton();


            MyRobot.move(imageLButton[0]+387-115,imageLButton[1]+39-123);
            MyRobot.lButton();

            //简单级别启动
            System.out.println("简单级别启动");
            int i = mouseResFor();
            if(i != 0){
                System.out.println("移动到X上点击关闭");
                MyRobot.move(imageLButton[0]+387-115+500,imageLButton[1]+39-123-66);
                MyRobot.lButton();
                MyRobot.move(imageLButton[0]+387-115+500-340,imageLButton[1]+39-123-66+250);
                findImageLButton("qd.png");
                continue;
            }
            //简单级别结束

            //点击继续
            MyRobot.move(imageLButton[0]+387-115+158,imageLButton[1]+39-123+181);
            MyRobot.lButton();

            //定位到头部开始
            MyRobot.move(imageLButton[0]+387-115,imageLButton[1]+39-123);
            MyRobot.lButton();
            MyRobot.sleep(1000);
            //第二轮启动识别
            System.out.println("第二轮启动识别");
            int ii = mouseResFor();
            if(ii != 0){
                System.out.println("移动到X上点击关闭");
                MyRobot.move(imageLButton[0]+387-115+500,imageLButton[1]+39-123-66);
                MyRobot.lButton();
                MyRobot.move(imageLButton[0]+387-115+500-340,imageLButton[1]+39-123-66+250);
                findImageLButton("qd.png");
                continue;
            }


            //点击继续
            MyRobot.move(imageLButton[0]+387-115+158,imageLButton[1]+39-123+181);
            MyRobot.lButton();

            //定位到头部开始
            MyRobot.move(imageLButton[0]+387-115,imageLButton[1]+39-123);
            MyRobot.lButton();
            MyRobot.sleep(1000);
            //第三轮
            System.out.println("第三轮启动识别");
            int iii = mouseResFor();
            if(iii != 0){
                System.out.println("移动到X上点击关闭");
                MyRobot.move(imageLButton[0]+387-115+500,imageLButton[1]+39-123-66);
                MyRobot.lButton();
                MyRobot.move(imageLButton[0]+387-115+500-340,imageLButton[1]+39-123-66+250);
                findImageLButton("qd.png");
                continue;
            }

            //点击确定 结束
            MyRobot.move(imageLButton[0]+387-115+243,imageLButton[1]+39-123+184);
            MyRobot.sleep(1000);
            MyRobot.lButton();
            MyRobot.lButton();
        }
    }
}
